package com.hellogood.yaohuo.server.mgr;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.QuyuOrg;
import com.hellogood.yaohuo.shared.entity.YhForm;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class YaohuoMgr {
	Logger log = Logger.getLogger(this.getClass());
	private String tableFormName = "YhForm";
	private String tableDetailName = "YhDetail";
	private String billNoField = "YhBillNo";
	private String dateField = "YhDate";

	public String createYaohuoForm(YhForm form) {
		String result = DAOFactory.getInstance().createYhFormDAO().createYaohuo(form, false);
		if (result.equals("")) {
			addLog(form.getBillNo(), false);
		}
		return result;
	}

	public String modifyYaohuoForm(YhForm form) {
		String result = DAOFactory.getInstance().createYhFormDAO().createYaohuo(form, true);
		if (result.equals("")) {
			addLog(form.getBillNo(), true);
		}
		return result;
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改要货单：" + billNo);
		} else {
			log.setContent("新建要货单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public int getLastYhBillNo(String billNo) {
		String no = DAOFactory.getInstance().createYhFormDAO().getLastBillNo(billNo, tableFormName, billNoField);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 4) {
				String lastNo = no.substring(no.length() - 4, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;
	}

	public List<Form> searchYhForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("0")) {
			return DAOFactory.getInstance().createYhFormDAO().searchFormByQuyu(tableFormName, billNoField, dateField, null, startDate, endDate, rz);
		} else {
			return DAOFactory.getInstance().createYhFormDAO().searchForm(tableFormName, billNoField, dateField, code, startDate, endDate, rz);
		}
		// if (level == 2) {
		// return
		// DAOFactory.getInstance().createYhFormDAO().searchFormByQuyu(tableFormName,
		// billNoField, dateField, code, startDate, endDate, rz);
		// } else if (level == 3) {
		//
		// } else if (level == 1) {
		//
		// }
	}

	public Form getFormDetail(String billNo) {
		Form form = DAOFactory.getInstance().createYhFormDAO().getFormByBillNo(tableFormName, billNoField, dateField, billNo);
		if (form != null) {
			List<Detail> list = DAOFactory.getInstance().createDetailDAO().getDetailByBillNo(billNo, tableDetailName, billNoField);
			form.setList(list);
		}
		return form;
	}

	public List<Detail> search(String quyuCode, String date) {
		List<QuyuOrg> list = MgrFactory.getInstance().createQuyuOrgMgr().getQuyuOrgByCode(quyuCode);
		String code = "";
		for (QuyuOrg q : list) {
			code += "," + q.getOrgCode();
		}

		if (code.substring(0, 1).equals(",")) {
			code = code.substring(1, code.length());
		}

		return DAOFactory.getInstance().createYhDetailDAO().searchDetail(code, date);
	}

	public String deleteYaohuo(String billNo) {
		return DAOFactory.getInstance().createYhFormDAO().deleteForm(tableFormName, tableDetailName, billNoField, billNo);
	}

	public List<Detail> getYaohuohuizong(String quyuCode, String date) {
		List<Detail> list = new ArrayList<Detail>();
		list = DAOFactory.getInstance().createYhDetailDAO().getYaohuohuizong(quyuCode, date);
		return list;
	}

	public String rz(String billNo) {
		return DAOFactory.getInstance().createYhFormDAO().rz(billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		for (String bill : bills) {
			String temp = DAOFactory.getInstance().createYhFormDAO().rz(bill);
			if (!temp.equals("")) {
				result = temp;
			}
		}
		return result;
	}
}
